<template>
  <div class="bigBox">
    <div class="topBox">
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            时间
          </div>
        </div>
        <div class="right" style="height: 100px;">
          <el-date-picker v-model="timeList" type="datetimerange" range-separator="~" @change="getTurnGoods"
            start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
          </el-date-picker>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            商品分类
          </div>
          <div>
            <el-dropdown @command="goodTypehandleCommand">
              <span class="el-dropdown-link">
                {{goodType}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="3">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-select v-model="formList.skuClass" placeholder="请选择商品分类" clearable :style="{ width: '100%' }" filterable
            @change="changeTypeId">
            <el-option v-for="item in goodsTypeList" :label="`${item.ncc_stu_name}(${item.ncc_stu_code})`"
              :value="item.pk_marbasclass" />
          </el-select>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            仓库
          </div>
          <div>
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                {{chooseSto}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="3">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-select v-model="formList.wareHouseName" placeholder="请选择仓库" clearable :style="{ width: '100%' }"
            filterable @change="getWareHouse">
            <el-option v-for="item in warehousesList" :label="item.cwarehouse_name" :value="item.cwarehouse_id" />
          </el-select>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            产品
          </div>
          <div>
            <el-dropdown @command="goodshandleCommand">
              <span class="el-dropdown-link">
                {{chooseSku}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="3">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-select v-model="formList.skuID" placeholder="请选择产品" clearable :style="{ width: '100%' }" filterable
            @change="getGoods">
            <el-option v-for="item in goodsTotalList" :label="item.material_name" :value="item.pk_material" />
          </el-select>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            批号
          </div>
          <div>
            <el-dropdown @command="batchhandleCommand">
              <span class="el-dropdown-link">
                {{batchName}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="3">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-input v-model="formList.batchNo" placeholder="请输入批号" style="width: 100%;"></el-input>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            商品状态
          </div>
        </div>
        <div class="right">
          <el-select v-model="formList.skuState" placeholder="请选择商品状态" clearable :style="{ width: '100%' }" filterable
            @change="goodsStateMethod">
            <el-option v-for="item in goodsState" :label="item.label" :value="item.value" />
          </el-select>
        </div>
      </div>
    </div>
    <el-row>
      <el-col :span="24">
        <div class="areaChart">
          <turnoverChart :turnChartNum="turnChartNum" />
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="areaChart">
          <turnoverTable :tableList="tableList" />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {
    getWarehouseStatistic, //获取统计数量
    getWarehouseBasic, //获取商品和仓库下拉
    findTurnoverStatistic
  } from "@/api/report/index.js";
  import turnoverTable from './other/turnoverTable.vue'
  import turnoverChart from './other/turnoverChart.vue'
  export default {
    name: 'totalBank',
    data() {
      return {
        formList: {
          beginDate: '',
          rndDate: '',
          skuClass: '',
          skuClassValue: 3,
          wareHouseName: '',
          wareHouseValue: 3,
          skuID: '',
          skuIDValue: 3,
          batchNo: '',
          batchNoValue: 3,
          skuState: '', //商品状态
        },
        timeList: [], //时间合计
        chooseSto: '全部', //仓库下拉
        chooseSku: '全部', //产品下拉
        batchName: '全部', //批号下拉
        goodType: '全部', //商品分类下拉
        warehousesList: [],
        goodsTotalList: [],
        goodsState: [{
          label: '全部',
          value: 0
        }, {
          label: '已完结',
          value: 1
        }, {
          label: '未完结',
          value: 2
        }], //获取商品状态
        goodsTypeList: [], //获取商品分类
        tableList: [], //库存明细数据
        turnChartNum: [] //前十名统计
      }
    },
    components: {
      turnoverTable,
      turnoverChart
    },
    created() {
      this.getData();
      this.getTurnGoods()
    },
    methods: {
      getTurnGoods() {
        if (this.timeList&&this.timeList[0]) {
          this.formList.beginDate = this.timeList[0];
        } else {
          delete this.formList.beginDate; // 如果时间为空，则删除该字段
        }

        if (this.timeList&&this.timeList[1]) {
          this.formList.rndDate = this.timeList[1];
        } else {
          delete this.formList.rndDate; // 如果时间为空，则删除该字段
        }
        findTurnoverStatistic(this.formList).then(res => {
          this.turnChartNum = res.data.type0 || [];
          this.tableList = res.data.type1 || [];
        })
      },
      //初始化基础数据
      getData() {
        getWarehouseBasic().then(res => {
          this.warehousesList = res.data.type1 || [];
          this.goodsTotalList = res.data.type2 || [];
          this.goodsTypeList = res.data.type3 || [];
        }).catch(error => {
          this.$message.error('获取仓库和产品数据失败:', error);
        });
      },
      //切换仓库
      getWareHouse(val) {
        this.formList.wareHouseName = val;
        this.getTurnGoods();
      },
      //切换产品
      getGoods(val) {
        this.formList.skuID = val;
        this.getTurnGoods();
      },
      //切换商品状态
      goodsStateMethod(val) {
        this.formList.skuState = val;
        this.getTurnGoods();
      },
      //切换商品分类
      changeTypeId(val) {
        this.formList.skuclassId = val;
        this.getTurnGoods();
      },
      //仓库下拉
      handleCommand(command) {
        switch (command) {
          case '1':
            this.chooseSto = '等于';
            this.formList.wareHouseValue = command;
            this.getTurnGoods();
            break;
          case '2':
            this.chooseSto = '不等于';
            this.formList.wareHouseValue = command;
            this.getTurnGoods();
            break;
          case '3':
            this.chooseSto = '全部';
            this.formList.wareHouseValue = command;
            this.getTurnGoods();
            break;
        }
      },
      //产品下拉
      goodshandleCommand(command) {
        switch (command) {
          case '1':
            this.chooseSku = '等于';
            this.formList.skuIDValue = command;
            this.getTurnGoods();
            break;
          case '2':
            this.chooseSku = '不等于';
            this.formList.skuIDValue = command;
            this.getTurnGoods();
            break;
          case '3':
            this.chooseSku = '全部';
            this.formList.skuIDValue = command;
            this.getTurnGoods();
            break;
        }
      },
      //批号下拉
      batchhandleCommand(command) {
        switch (command) {
          case '1':
            this.batchName = '等于';
            this.formList.batchNoValue = command;
            this.getTurnGoods();
            break;
          case '2':
            this.batchName = '不等于';
            this.formList.batchNoValue = command;
            this.getTurnGoods();
            break;
          case '3':
            this.batchName = '全部';
            this.formList.batchNoValue = command;
            this.getTurnGoods();
            break;
        }
      },
      //商品分类下拉
      goodTypehandleCommand(command) {
        switch (command) {
          case '1':
            this.goodType = '等于';
            this.formList.skuClassValue = command;
            this.getTurnGoods();
            break;
          case '2':
            this.goodType = '不等于';
            this.formList.skuClassValue = command;
            this.getTurnGoods();
            break;
          case '3':
            this.goodType = '全部';
            this.formList.skuClassValue = command;
            this.getTurnGoods();
            break;
        }
      },

    },
  }
</script>
<style scoped lang="scss">
  .bigBox {
    padding: 20px;
  }

  .topBox {
    flex-wrap: wrap;
    display: flex;
    gap: 0 15px;
  }

  //曲线图
  .areaChart {
    border: 1px solid #e1e1e1;
    padding: 10px;
    border-radius: 10px;
    box-shadow: 5px 5px 10px 0px rgba(0, 0, 0, 0.1);
  }

  .blueLine {
    display: flex;
    align-items: center;
  }

  //下拉菜单
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }

  .blueLine:before {
    content: "";
    background: #8cc5ff;
    display: block;
    width: 6px;
    height: 20px;
    margin-right: 10px;

  }

  .leftBox:nth-child(2n) .right {
    display: flex;
    align-items: center;
    justify-content: space-around;
  }

  .imgBox {
    width: 50px;
    height: 50px;
    border-radius: 5px;
    background-color: #eef7ff;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .leftBox {
    margin-bottom: 10px;
    padding: 0 10px;
    height: 150px;
    width: calc(16.67% - 13px);
    border-radius: 10px;
    border: 1px solid #e1e1e1;
    box-shadow: 5px 5px 10px 0px rgba(0, 0, 0, 0.1);
    flex-wrap: wrap;

    .title {
      justify-content: space-between;
      display: flex;
      align-items: center;
      color: #333333;
      font-weight: bold;
      padding: 10px 0px;
      font-size: 18px;
    }

    .right {
      display: flex;
      align-items: center;
      justify-content: space-evenly;
      font-size: 30px;
      color: #409EFF;
      line-height: 100px;
      font-weight: bold;
    }
  }

  .el-row {
    margin-top: 10px;
  }

  ::v-deep .el-input--suffix {
    height: 70px;
  }

  @media (max-width: 1200px) {
    .leftBox {
      width: calc(25% - 13px);
      /* 每行4个卡片 */
    }
  }

  @media (max-width: 768px) {
    .leftBox {
      width: calc(50% - 13px);
      /* 每行2个卡片 */
    }
  }

  @media (max-width: 480px) {
    .leftBox {
      width: 100%;
      /* 每行1个卡片 */
    }
  }
</style>
